<?php

namespace App\Http\Requests;

use App\Http\Requests\Request;

class RecommendRequest extends Request
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $data = Request::all();
        $editRules = [
            'id'       => 'required|numeric',
            'subject'  => 'required',
            'position' => 'required|in:web_index,web_index_road',
            'photo'    => 'sometimes',
            'table'    => 'required|in:article,project,activity,video',
            'post_id'  => 'required|numeric',
            'sort'     => 'required|numeric',
            'path_url' => 'sometimes|url',
            'status'   => 'required|boolean',
        ];

        $createRules = [
            'subject'  => 'required',
            'position' => 'required|in:web_index,web_index_road',
            'photo'    => 'sometimes',
            'table'    => 'required|in:article,project,activity,video',
            'post_id'  => 'required|numeric',
            'sort'     => 'required|numeric',
            'path_url' => 'sometimes|url',
            'status'   => 'required|boolean',
        ];

//        return array();


        //如果是编辑
        if ($data['formType'] == 'edit') {
            return $editRules;
        }
        //如果是创建
        if ($data['formType'] == 'create') {
            return $createRules;
        }
    }

    public function attributes()
    {
        $attributes = array(
            'subject'  => '标题',
            'position' => '推荐的位置',
            'photo'    => '推荐图片',
            'table'    => '所处的表格',
            'post_id'  => '表格中的标题',
            'sort'     => '排序',
            'path_url' => '跳转地址',
            'status'   => '是否生效',
        );

        return $attributes;
    }

    public function messages()
    {
        $messages = [
            'required'       => ':attribute为必填选项',
            'numeric'        => ':attribute必须为数字',
            'position.in'    => ':attribute必须为网站首页热门栏目、网站首页在路上栏目中的一种',
            'table.in'       => ':attribute必须为活动、文章、项目、视频中的一种',
            'url'            => ':attribute必须为合乎规范的url地址',
            'status.boolean' => ':attribute必须为生效、暂不生效的一种',
        ];

        return $messages;
    }
}
